/* << fastinsert report library >> OciBase.h  Nov 1, 2010
 *
 * 	http://code.google.com/p/libfir
 *
 * Copyright (c) 2005 Pavlo Drobov (drobov.pavel@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */


/*
 * ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA
 * ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA
 * ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA
 * ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA
 * ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA
 * ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA
 * ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA
 * ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA
 * ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA
 * ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA
 * ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA
 * ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA ALFA
 */


#ifndef OCIBASE_H_
#define OCIBASE_H_

#include "GridOci.h"

#include "IGrid.h"
#include "IPhysicFormat.h"
#include "Text.h"


namespace dasco
{
namespace report
{

/////////////////////////////////////////////////////////////////
#define DASCO_DEFAULT_OCI_CHARSET 	"CL8MSWIN1251"


//! \class OciException
//! \brief Oci specific Exception
class OciException
	: public IIOException
	, public Text
{
public:
	OciException(const char*,errorNumber);
	virtual ~OciException(){}
};

//! \struct OciConnect
//! \brief connect string to oracle
struct DLL_EXPORT OciConnect
	: IConnect
{
	char login[0xFF];
	char passwd[0xFF];
	char db[0x1FF];
	const char *charset;

	OciConnect(const char*,const char*,const char*,const char* = DASCO_DEFAULT_OCI_CHARSET);
	virtual ~OciConnect(){};
	const char* getLogin() const;
	const char* getPassword() const;
	const char* getDB() const;

	friend Debug& operator << (Debug& debug, OciConnect& conn)
	{
		debug << conn.getLogin() << "/" << conn.getPassword() << "@" << conn.getDB();
		return debug;
	}

};

//! \struct MyOciDefine
//! \brief OciDefine wrapper
struct MyOciDefine
{
	OCIDefine 	* define;
	ub2         col_type, col_width, col_precesion, col_scale, col_order;
	ub4         col_name_len, char_semantics;
    ub2   		alenname;           // return lengths
	ub2   		rcodename;		  // return codes
	text        *col_name;


	operator const char*() { return (char*) col_name;}
	void init(ub2,OCIParam*);

	AOciCell *defineByPos(OCIStmt*);

private:
	AOciCell *ociCellFactory() const;
};


class  DLL_EXPORT OciBase
	:public IPhysicFormat
{
public:
	OciBase(const OciConnect&);
	virtual ~OciBase();


	void renderGrid(const IGrid*);
	void save(const char*) throw (OciException);
	void load(const char*,IGrid***,unsigned*) throw (OciException);

	//multisql document, NULL terminated SQL array
	void load(const char* const *,IGrid***,unsigned*) throw (OciException);
	void connect(const IConnect*) throw (OciException);

//protected:
	IGrid* callSQL(const char*)  throw (OciException);

private:

};

}} //end namespace dasco::report

#endif /* OCI_H_ */
